home *** CD-ROM | disk | FTP | other *** search
- Path: s02.pavilion.co.uk!usenet
- From: AJRobb@pavilion.co.uk (Andy J Robb)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer arithmetic
- Date: Fri, 01 Mar 1996 06:56:58 GMT
- Organization: Pavilion Internet plc
- Message-ID: <4h672i$329@s02.pavilion.co.uk>
- References: <4h2r55$er@s3.iway.fr>
- NNTP-Posting-Host: poolc53.pavilion.co.uk
- X-Newsreader: Forte Free Agent 1.0.82
-
- Pascal Terracol <assetsto@pratique.fr> wrote:
-
- > Hello,
-
- >this sample code have been correctly working on a pc 80286 processor
-
- >I put it on a mac and the pointers seems to act differently...
- >any idea about that ?
-
- > int size, n1, n2 ;
- > Point *p_debut, *p1, *p2 ; /* "vecteur" de translation des adr */
-
- I'm assuming that l includes pointers:
-
- struct {
- Point *p1, *p2;
- ...
- } *l;
-
- >...
-
- > n1 = (int) (l->p1)/sizeof(Point) ;
- > n2 = (int) (l->p2)/sizeof(Point) ;
- >
- > p1 = p_debut + n1 ;
- > p2 = p_debut + n1 ;
- >...
-
- To make this work you need a datum for l->p1 and l->p2. The above
- code implicitly uses NULL as the datum (an unlikely event). It does
- not have to work under ISO/ANSI (it may have worked under K&R where
- int and pointers were interchangable). Syntactically, what you have
- is similar to:
-
- n1 = l->p1 - (Point*)0;
- n2 = l->p2 - (Point*)0;
-
- If you were to include a datum (Point *p_debut;) within *l, then:
-
- n1 = l->p1 - l->p_debut;
- n2 = l->p2 - l->p_debut;
-
- As an asside, be aware that the whole ethos of this pointer arithmetic
- for non-simple types becomes dubious under C++ (as sizeof(Point) may
- not be the size of the element which Point* is pointing to.) To be
- "safe" you should define an array of pointers (which are a simple type
- and, therefore, safe to use with indexing arithmetic) and initialize
- each of these to point to individual structures. The extra level of
- dereferencing will be offset by the simpler pointer arithmetic.
-
- Au revoir,
-
-
- -----BEGIN PGP PUBLIC KEY BLOCK-----
- Version: 2.6.2i
-
- mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
- MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
- B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
- tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
- IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
- =/wVD
- -----END PGP PUBLIC KEY BLOCK-----
-
-